<?php
App::uses('AppController', 'Controller');
/**
 * teams Controller
 *
 * @property team $team
 */
class TeamsController extends AppController {
	public $components = array('RequestHandler', 'AjaxMultiUpload.Upload');
	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		
		$this->set('teams', $this->paginate());

	}

	public function addDiscipline(){
		$this->loadModel('Discipline');
		$disciplines = $this->Discipline->find('all', array('fields' => array('DISTINCT Discipline.name')));
		$data = array();
		foreach ($disciplines as $key => $value) {
			if($value['Discipline']['name'])
				$data[] = array('Team' => array('name' => $value['Discipline']['name']));
			
		}
		$this->Team->saveMany($data);
		
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		$this->Team->id = $id;
		if (!$this->Team->exists()) {
			throw new NotFoundException(__('Invalid discipline'));
		}
		$this->set('team', $this->Team->read(null, $id));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Team->create();
			$this->request->data['Team']['_user'] = AuthComponent::user('id');
			if ($this->Team->save($this->request->data)) {
				$this->Session->setFlash(__('The discipline has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The discipline could not be saved. Please, try again.'));
			}
		}
	}

	public function beforeFilter(){
		$this->isAuthorized(array('admin', 'coordinator'));
	}

	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function edit($id = null) {
		$this->Team->id = $id;
		if (!$this->Team->exists()) {
			throw new NotFoundException(__('Invalid discipline'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['Team']['_user'] = AuthComponent::user('id');
			if ($this->Team->save($this->request->data)) {
				$this->Session->setFlash(__('The discipline has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The discipline could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Team->read(null, $id);
		}
	}

	/**
	 * delete method
	 *
	 * @throws MethodNotAllowedException
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function delete($id = null) {
		
			if (!$this->request->is('post')) {
				throw new MethodNotAllowedException();
			}
			$this->Team->id = $id;
			if (!$this->Team->exists()) {
				throw new NotFoundException(__('Invalid discipline'));
			}
			if ($this->Team->delete()) {
				$this->Session->setFlash(__('Discipline deleted'));
				$this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('Discipline was not deleted'));
			$this->redirect(array('action' => 'index'));
		
	}

}
